package com.controller;

import com.Exception.UserException;
import com.pojo.Result;
import com.pojo.User;
import com.service.UserService;
import com.validation.UGroup1;
import com.validation.UserValidator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.DataBinder;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Controller
@RequestMapping("/home")
public class IndexController {

    //这个方法指调用自定义的Validation
    @InitBinder
    public void initBinder(DataBinder binder){
        binder.setValidator(new UserValidator());
    }

    @RequestMapping("/index")
    public String index(){
        return "views/home";
    }

    @RequestMapping("/login")
    public String login(){
        return "views/login";
    }
    //这个登入接口不正确
    //@RequestMapping("/loginJson")
    public Result login(HttpServletRequest request, HttpServletResponse response) throws Exception {

        // 1.0 获取登录参数
        String szLoginName = request.getParameter("username");
        String szPassWord = request.getParameter("password");
        //String data = request.getParameter("data");
        if (StringUtils.isEmpty(szLoginName) || StringUtils.isEmpty(szPassWord)) {
            return Result.error();
        }
        if(szLoginName.equals("sa") && szPassWord.equals("123456")){
            String str = request.getContextPath() + "/views/menu.jsp";
            //request.setAttribute("user",new User(szLoginName,szPassWord));
            //request.getRequestDispatcher(request.getContextPath()+"/home/welcome").forward(request,response);
            ModelAndView mv = new ModelAndView();
            //mv.setViewName("welcome");
            return Result.success();
        }
        User user = new User();
        //request.getSession().setAttribute("userInfo", userInfo);
        return Result.error();
    }

    //登入接口
//    @RequestMapping("/loginDo")
    public String logindo(HttpServletRequest request) throws Exception {

        // 1.0 获取登录参数
        String szLoginName = request.getParameter("username");
        String szPassWord = request.getParameter("password");
        //String data = request.getParameter("data");
        if (StringUtils.isEmpty(szLoginName) || StringUtils.isEmpty(szPassWord)) {
            return "views/loginError";
        }
        if(szLoginName.equals("sa") && szPassWord.equals("123456")){

            return "views/menu";
        }
        User user = new User();
        //request.getSession().setAttribute("userInfo", userInfo);
        return "404";
    }
    //@Validated 与  BindingResult 是成对出现的  一前一后
    //Value=group1.class  分组
    @RequestMapping(value = "/loginDo",method = RequestMethod.POST)
    public String loginDo(Model model,HttpServletRequest request,
                          @Validated(value = UGroup1.class) User user , BindingResult bindingResult) throws UserException {
        //获取校验错误信息
        List<ObjectError> allErrors = null;
        if(bindingResult.hasErrors()){
            allErrors = bindingResult.getAllErrors();
            for(ObjectError objectError : allErrors){
                //输出错误信息
                System.out.println(objectError.getDefaultMessage());
            }
        }
        UserService userService = new UserService();
        if(user != null){
            if(userService.login(user)){
                request.getSession().setAttribute("user",user);
                return "views/menu";
            }
        }
        return "404";
    }

}
